﻿using Inovout.Data.Repository;
using System;
using System.Linq;
using System.Text;

namespace Inovout.Membership.Security
{
    [Component.Component]
    public class DynamicPasswordCredentialProvider
    {
        private IRepository<DynamicPassword> dynamicPasswordRepository;
        private IRepository<DynamicPasswordCredential> dynamicPasswordCredentialRepository;

        public DynamicPasswordCredentialProvider()
        {
            this.dynamicPasswordRepository = Component.ComponentRegistry.Resolve<IRepository<DynamicPassword>>();
            this.dynamicPasswordCredentialRepository = Component.ComponentRegistry.Resolve<IRepository<DynamicPasswordCredential>>();
         }

        public DynamicPasswordCredential Validate(string phone, string code)
        {
            //Q 不能使用Id,因为Id是动态类型。
            DynamicPassword dynamicPassword = dynamicPasswordRepository.Query().SingleOrDefault(c => c.Phone == phone && c.Code == code);
            if (dynamicPassword == null)
            {
                return null;
            }
            //验证密码是否过期
            bool result = dynamicPassword.StartTime.AddSeconds(dynamicPassword.Duration) < DateTime.Now;

            DynamicPasswordCredential dynamicPasswordCredential = null;
            if (!result)
            {
                dynamicPassword.State = DynamicPasswordState.Verified;
                dynamicPassword.ResultState = ValidateResult.success;
                dynamicPasswordCredential = dynamicPasswordCredentialRepository.Query().SingleOrDefault(c => c.Phone == phone) ?? new DynamicPasswordCredential(dynamicPassword.Phone);
            }
            else
            {
                dynamicPassword.State = DynamicPasswordState.expire;
                dynamicPassword.ResultState = ValidateResult.fail;
            }
            dynamicPasswordRepository.Save(dynamicPassword);
            return dynamicPasswordCredential;
        }

        public DynamicPassword GetDynamicPassword(string phone)
        {
            DynamicPassword dynamicPassword = dynamicPasswordRepository.Query().SingleOrDefault(c => c.Phone == phone && c.State == DynamicPasswordState.NotVerified) ?? new DynamicPassword();
            dynamicPassword.Phone = phone;
            dynamicPassword.StartTime = DateTime.Now;
            dynamicPassword.Code = GenerateRandomNumber(6);

            dynamicPassword.Duration = 60;
            dynamicPassword.State = DynamicPasswordState.NotVerified;
            dynamicPassword.ResultState = ValidateResult.fail;
            dynamicPasswordRepository.Save(dynamicPassword);

            return dynamicPassword;

        }


        private string GenerateRandomNumber(int Length)
        {
            StringBuilder newRandom = new StringBuilder(Length);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(rd.Next(9).ToString());
            }
            return newRandom.ToString();
        }

    }
}
